package com.example.lanouhn.firstapp.activity.adapter;

import android.content.Context;
import android.graphics.Paint;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.lanouhn.firstapp.R;
import com.example.lanouhn.firstapp.activity.model.BrokerDealer;
import com.squareup.picasso.Picasso;

import java.util.List;

/**
 * 首页自营按钮跳转到商城页面的数据适配器
 */
public class BrokerDealerAdapter extends RecyclerView.Adapter {

    //上下文对象
    private Context context;
    //数据集合
    private List<BrokerDealer> list;

    //定义监听接口
    public interface BrokerDealerRecyclerViewListener{

        void onClick(View view,int position);
    }

    private BrokerDealerRecyclerViewListener brokerDealerRecyclerViewListener;

    public void setBrokerDealerRecyclerViewListener(BrokerDealerRecyclerViewListener brokerDealerRecyclerViewListener) {
        this.brokerDealerRecyclerViewListener = brokerDealerRecyclerViewListener;
    }



    public BrokerDealerAdapter(List<BrokerDealer> list, Context context) {
        this.list = list;
        this.context = context;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        //加载布局
        RecyclerView.ViewHolder holder = null;
        holder = new VHolder(
                LayoutInflater.from(context).

                        inflate(R.layout.brokedealer_item_list,null));

        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {

        final VHolder vHolder = (VHolder) holder;
        //数据位置
        BrokerDealer brokerdealer = list.get(position);
        //设置数据  运用picasso获取图片
        Picasso.with(context).load(brokerdealer.getUrl()).into(vHolder.imageView);
        vHolder.tv_name.setText(brokerdealer.getGoods_name());
        vHolder.tv_price.setText(brokerdealer.getGoods_price());
        vHolder.tv_marketPrice.setText(brokerdealer.getGoods_marketprice());
        vHolder.tv_number.setText(brokerdealer.getIs_presell());
        //中划线
        vHolder.tv_marketPrice.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
        //recyclerView监听事件
        vHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                brokerDealerRecyclerViewListener.onClick(vHolder.itemView,position);
            }
        });
    }


    @Override
    public int getItemCount() {
        return list.size();
    }


    //定义VHolder类继承RecyclerView.ViewHolder
    class VHolder extends RecyclerView.ViewHolder{

        private ImageView imageView;
        private TextView tv_name;
        private TextView tv_price;
        private TextView tv_marketPrice;
        private TextView tv_number;
        //绑定控件id
        public VHolder(View itemView) {
            super(itemView);
            imageView = (ImageView) itemView.findViewById(R.id.imageview);
            tv_name = (TextView) itemView.findViewById(R.id.tv_name);
            tv_price = (TextView) itemView.findViewById(R.id.tv_price);
            tv_marketPrice = (TextView) itemView.findViewById(R.id.tv_marketprice);
            tv_number = (TextView) itemView.findViewById(R.id.tv_number);

        }
    }
}
